#ifndef LGE_MUTEX_H
#define LGE_MUTEX_H

#include <core/lge_uncopyable.h>
#include <windows.h>

/**
  * \brief A mutex class
  *
  * This is a synchronization primitive, not copyable since everybody should
  * share this instance and not just a copy.
  */
class Mutex : public Uncopyable
{
	/**
	  * \brief Underlying handle (OS dept)
	  */
	HANDLE mMutexHandle;

public:

	/**
	  * \brief Create a new mutex, unlocked
	  */
	Mutex()
	{
		mMutexHandle = CreateMutex(NULL, FALSE, NULL);
	}

	/**
	  * \brief Lock the mutex
	  */
	inline void lock()
	{
		WaitForSingleObject(mMutexHandle, INFINITE);
	}

	/**
	  * \brief Unlock the mutex
	  *
	  * \return true on success, false otherwise
	  */
	inline bool unlock()
	{
		return (ReleaseMutex(mMutexHandle) != 0);
	}

	/**
	  * \brief Destroy the mutex and all associated resources
	  */
	~Mutex()
	{
		CloseHandle(mMutexHandle);
	}
};

#endif
